<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common"
 >
  <xsl:output method="html"/>
  <xsl:template match="/">
    <h1>
      Metric Report:
    </h1>
    <xsl:call-template name="Total" />

    <h2>Modules:</h2>
    <table border="1" cellSpacing="0" cellPadding="5" >

      <thead style="text-align: center;">
        <td>Module</td>
        <td style="background-color: forestGreen; color:white">Maintainability Index</td>
        <td style="background-color: darkblue; color: white;">Cyclomatic Complexity</td>
        <td style="background-color: forestGreen; color: white;">Depth Of Inheritance</td>
        <td style="background-color: darkblue; color:white;">Class Coupling</td>
        <td style="background-color: forestGreen; color: white;">Lines Of Code</td>
      </thead>
      <xsl:apply-templates select="//CodeMetricsReport" />
    </table>
  </xsl:template>
  
  <xsl:template name="Total">
    <h2>Total:</h2>
    <table border="1" cellSpacing="0" cellPadding="5" >
      <thead style="text-align: center;">
        <td style="background-color: forestGreen; color:white">Maintainability Index</td>
        <td style="background-color: darkblue; color: white;">Cyclomatic Complexity</td>
        <td style="background-color: forestGreen; color: white;">Depth Of Inheritance</td>
        <td style="background-color: darkblue; color:white;">Class Coupling</td>
        <td style="background-color: forestGreen; color: white;">Lines Of Code</td>
      </thead>
      <xsl:variable name="MaintainabilityIndex">
        <xsl:for-each select="//Modules/Module/Metrics/Metric[@Name = 'MaintainabilityIndex']">
          <Metric Value="{translate(@Value, ',', '')}"/>
        </xsl:for-each>
      </xsl:variable>

      <xsl:variable name="CyclomaticComplexity">
        <xsl:for-each select="//Modules/Module/Metrics/Metric[@Name = 'CyclomaticComplexity']">
          <Metric Value="{translate(@Value, ',', '')}"/>
        </xsl:for-each>
      </xsl:variable>

      <xsl:variable name="ClassCoupling">
        <xsl:for-each select="//Modules/Module/Metrics/Metric[@Name = 'ClassCoupling']">
          <Metric Value="{translate(@Value, ',', '')}"/>
        </xsl:for-each>
      </xsl:variable>

      <xsl:variable name="DepthOfInheritance">
        <xsl:for-each select="//Modules/Module/Metrics/Metric[@Name = 'DepthOfInheritance']">
          <Metric Value="{translate(@Value, ',', '')}"/>
        </xsl:for-each>
      </xsl:variable>

      <xsl:variable name="LinesOfCode">
        <xsl:for-each select="//Modules/Module/Metrics/Metric[@Name = 'LinesOfCode']">
          <Metric Value="{translate(@Value, ',', '')}"/>
        </xsl:for-each>
      </xsl:variable>


      <td style="background-color: forestGreen; color: white;">
        <xsl:value-of select="sum(ext:node-set($MaintainabilityIndex)/*/@Value)" />
      </td>
      <td style="background-color: darkblue; color: white;">
        <xsl:value-of select="sum(ext:node-set($CyclomaticComplexity)/*/@Value)" />
      </td>
      <td style="background-color: forestGreen; color: white;">
        <xsl:value-of select="sum(ext:node-set($ClassCoupling)/*/@Value)" />
      </td>
      <td style="background-color: darkblue; color: white;">
        <xsl:value-of select="sum(ext:node-set($DepthOfInheritance)/*/@Value)" />
      </td>
      <td style="background-color: forestGreen; color: white;">
        <xsl:value-of select="sum(ext:node-set($LinesOfCode)/*/@Value)" />
      </td>
    </table>
  </xsl:template>
  
  <xsl:template match="CodeMetricsReport">
    <xsl:apply-templates select="Targets/Target/Modules/Module" />
  </xsl:template>

  <xsl:template match="//CodeMetricsReport/Targets/Target/Modules/Module">
    <tr style="text-align: center;">
      <td style="text-align: left;">
        <xsl:value-of select="@Name" />
      </td>
      <xsl:for-each select="Metrics/Metric">
        <xsl:if test="@Name='MaintainabilityIndex'">
          <td style="background-color: forestGreen; color: white;">
            <xsl:value-of select="@Value" />
          </td>
        </xsl:if>
        <xsl:if test="@Name='CyclomaticComplexity'">
          <td style="background-color: darkblue; color: white;">
            <xsl:value-of select="@Value" />
          </td>
        </xsl:if>
        <xsl:if test="@Name='ClassCoupling'">
          <td style="background-color: forestGreen; color: white;">
            <xsl:value-of select="@Value" />
          </td>
        </xsl:if>
        <xsl:if test="@Name='DepthOfInheritance'">
          <td style="background-color: darkblue; color: white;">
            <xsl:value-of select="@Value" />
          </td>
        </xsl:if>
        <xsl:if test="@Name='LinesOfCode'">
          <td style="background-color: forestGreen; color: white;">
            <xsl:value-of select="@Value" />
          </td>
        </xsl:if>
      </xsl:for-each>
    </tr>
  </xsl:template>
</xsl:stylesheet>
